<?php
set_time_limit(0);
require_once 'config.php';
require_once 'conndb.php';
require_once 'lib/amazon/amazon_api_class.php';
require_once 'lib/xmlrpc.php';
require_once 'domain/wordpress.domain.php';
require_once 'domain/wordpress_xmlrpc.domain.php';
require_once 'domain/category.domain.php';
require_once 'domain/customfield.domain.php';
require_once 'domain/amazon.domain.php';
require_once 'domain/item.domain.php';
require_once 'domain/category.domain.php';

$wplist  = Wordpress::getByObj(new Wordpress());
$catelist = Category::getByObj(new Category());

?>
<?php require_once 'template/header.php'; ?>

<form id="form" action="#" method="post">
    <div style="margin: 0 auto;width: 80%;color: red;text-align: center"><?=$errMsg?></div>
    <div style="margin: 0 auto;width: 80%;color: green;text-align: center"><?=$msg?></div>
    <fieldset style="background: #F2F2F2;"><legend><h3>Post Data</h3></legend>
        <table>
            <tr>
                <td>Wordpress : </td>
                <td>
                    <select id="wordpress" name="wordpress">
                        <option value=""></option>
                        <?php foreach ($wplist as $val) { ?>
                        <option value="<?php echo $val['ID']; ?>"><?php echo $val['wp_name']; ?></option>
                            <?php } ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Category : </td>
                <td>
                    <select id="category" name="category">
                        <option value=""></option>
                        <?php foreach ($catelist as $val) { ?>
                        <option value="<?php echo $val['ID']; ?>"><?php echo $val['category_name']; ?></option>
                            <?php } ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Number Of Post : </td>
                <td>
                    <select id="num_of_post" name="num_of_post">
                        <option></option>
                        <option value="1">1</option>
                        <option value="10">10</option>
                        <option value="100">100</option>
                        <option value="1000">1000</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>&nbsp</td>
                <td>
                    <input type="submit" value="POST DATA" />
                </td>
            </tr>
        </table>
    </fieldset>
</form>

<table>
    <tr>
        <td align="center">

            <?php
            if($_POST) {

                $wordpress = $_POST['wordpress'];
                $category = $_POST['category'];
                (int)$num_of_post = $_POST['num_of_post'];
                try {
                    if($num_of_post > 0) {
                        $objWordpress = new Wordpress();
                        $wordpressList = $objWordpress->getByID($wordpress);

                        $objCustomField = new Customfield();
                        $objCustomField->setWordpressId($wordpress);
                        $customList = $objCustomField->getByObj($objCustomField);

                        $objAmazon = new Amazon();
                        $objAmazon->setCategory($category);
                        $objAmazon->setFlag(' ');
                        $amazonList = $objAmazon->getByObj($objAmazon);

                        echo '*** Start Connect AmazonAPI ***<br>';
                        flush();
                        $amazonAPI = new AmazonProductAPI();
                        $amazonAPI->setAssociateTag(AWS_TAG);
                        $amazonAPI->setPublicKey(AWS_PUBLIC_KEY);
                        $amazonAPI->setPrivateKey(AWS_PRIVATE_KEY);
                        echo '*** Connected To AmazonAPI ***<br>';

                        /*** Get Category ***/
                        $categoryObj = new Category();
                        $categoryName = $categoryObj->getById($category);
                        
                        if(count($amazonList) == 0) {
                            throw new Exception("No Data");
                        }
                        if(count($amazonList) <= $num_of_post) {
                            $num_of_post = count($amazonList);
                        }
                        echo '*** Start Post To Wordpress ***<br>';
                        for($i = 0; $i < $num_of_post; $i++) {
                            $xml = $amazonAPI->getItemByAsin($amazonList[$i]['asin']);

                            $items = new Item();
                            $items->convert($xml);

                            $wordpressXMLRCP = new WordpressXMLRPC();
                            $wordpressXMLRCP->wp_username = $wordpressList[0]['wp_username'];
                            $wordpressXMLRCP->wp_password = $wordpressList[0]['wp_password'];
                            $wordpressXMLRCP->wp_url = $wordpressList[0]['wp_url'];
                            $wordpressXMLRCP->wp_template = $wordpressList[0]['wp_template'];
                            $wordpressXMLRCP->wp_category = $categoryName[0]['category_name'];


                            echo ($i+1) .' ' . $wordpressXMLRCP->post($items, $customList) . '<br>';
                            flush();
                        }

                    }
                }catch (Exception $e) {
                    echo '***'.$e->getMessage().'***';
                }

            }
            ?>

        </td>
    </tr>
</table>
<?php require_once 'template/footer.php'; ?>
<?php require_once 'closedb.php'; ?>

<script type="text/javascript">
    $(document).ready(function() {

        $("#form").validate({
            rules: {
                wordpress: "required",
                category: "required",
                num_of_post: "required"
            }
        });
    });
</script>